package com.framework.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.entity.dto.SysLogDTO;
import com.framework.system.entity.SysLog;
import com.framework.system.entity.dto.SysLogQueryDTO;
import com.framework.system.entity.vo.SysLogListVO;
import com.framework.system.entity.vo.SysLogVO;
import com.framework.system.mapper.SysLogMapper;
import com.framework.system.service.ISysLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 操作日志表  服务实现类
 * </p>
 *
 * @author Kevin
 * @since 2021-07-12
 */
@Service
@AllArgsConstructor
public class SysLogServiceImpl implements ISysLogService {

    private SysLogMapper sysLogMapper;

    @Override
    public Page<SysLogListVO> selectPage(Page reqPage, SysLogQueryDTO req) {
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(req.getLogModule()), SysLog::getLogModule, req.getLogModule());
        queryWrapper.like(StrUtil.isNotBlank(req.getLogTitle()), SysLog::getLogTitle, req.getLogTitle());
        queryWrapper.like(StrUtil.isNotBlank(req.getLogStatus()), SysLog::getLogStatus, req.getLogStatus());
        queryWrapper.like(StrUtil.isNotBlank(req.getUserPlatform()), SysLog::getUserPlatform, req.getUserPlatform());
        queryWrapper.like(StrUtil.isNotBlank(req.getOperName()), SysLog::getOperName, req.getOperName());
        queryWrapper.orderByDesc(SysLog::getCreateTime);
        Page<SysLog> page = sysLogMapper.selectPage(reqPage, queryWrapper);
        Page<SysLogListVO> voPage = BeanUtil.copyProperties(page, Page.class);
        voPage.setRecords(BeanUtil.copyToList(page.getRecords(), SysLogListVO.class));
        return voPage;
    }

    @Override
    public SysLogVO view(String id) {
        SysLog entity = sysLogMapper.selectById(id);
        return BeanUtil.copyProperties(entity, SysLogVO.class);
    }

    @Override
    public void insertSysLog(SysLogDTO req) {
        SysLog entity = BeanUtil.copyProperties(req, SysLog.class);
        sysLogMapper.insert(entity);
    }

}
